# WeaponBase.gd
class_name WeaponBase
extends Node2D

# 武器基本属性
@export var weapon_id: String = ""

@export_range(0.1, 10.0) var attack_cooldown: float = 1.0

@export_range(1, 100) var attack_power: int = 10

# 内部状态
var weapon_name: String = ""
var weapon_type: int    = -1
var can_attack: bool    = true

# 组件引用
@onready var cooldown_timer: Timer = $CooldownTimer
# 信号
signal attack_performed(weapon)
signal attack_finished(weapon)


func _ready() -> void:
	baseReady()


func baseReady()->void:
	# 加载武器配置数据
	load_weapon_data()

	# 设置冷却时间
	cooldown_timer.wait_time = attack_cooldown

	# 连接信号
	if not cooldown_timer.timeout.is_connected(_on_cooldown_timer_timeout):
		cooldown_timer.timeout.connect(_on_cooldown_timer_timeout)


# 加载武器数据 - 子类应重写此方法
func load_weapon_data() -> void:
	var data = WeaponDataManager.get_weapon_data(weapon_id)
	if data.is_empty():
		return

	weapon_name = data.get("name", "未命名武器")
	attack_power = data.get("damage", 10)
	attack_cooldown = data.get("attack_cooldown", 1.0)
	weapon_type = WeaponDataManager.get_weapon_type(weapon_id)


# 攻击方法 - 由子类实现
func attack() -> void:
	if not can_attack:
		return

	can_attack = false
	cooldown_timer.start()

	# 发送攻击开始信号
	emit_signal("attack_performed", self)


# 子类需要在完成攻击动作后调用 _finish_attack()

# 完成攻击
func _finish_attack() -> void:
	emit_signal("attack_finished", self)


# 冷却时间结束
func _on_cooldown_timer_timeout() -> void:
	can_attack = true


# 获取攻击方向 - 子类可覆盖
func get_attack_direction() -> Vector2:
	return Vector2.RIGHT


# 获取面向左侧标志 - 从WeaponHolder获取
func is_facing_left() -> bool:
	var weapon_holder: Node = get_parent()
	if weapon_holder and "facing_left" in weapon_holder:
		return weapon_holder.facing_left
	return false


# 提供给WeaponHolder翻转用的方法
func handle_flip(_flip_h: bool) -> void:
	# 子类应该实现这个方法来处理翻转
	pass
